﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using NGinnBPM.Lib.Interfaces;
using NGinnBPM.MessageBus.Impl.HttpService;
using NGinnBPM.MessageBus;
using System.IO;
using NGinnBPM.Lib.Schema;

namespace NGinnBPM.Services.Website
{
    [UrlPattern(@"^/schema/((?<defid>[\w\.]+)/(?<taskid>[\w\.]+)(/(?<dir>(in|out|internal)))?)?")]
    public class XmlSchemaServlet : ServletBase
    {
        public IProcessPackageRepository PackageRepository { get; set; }

        public override void HandleRequest(IRequestContext ctx)
        {
            string defid = ctx.UrlVariables["defid"];
            string taskid = ctx.UrlVariables["taskid"];
            string dir = ctx.UrlVariables["dir"];
            if (defid != null && defid.Length > 0)
            {
                ctx.ResponseContentType = "text/xml";
                ProcessDefinition pd = PackageRepository.GetProcess(defid);
                if (dir == null || dir.Length == 0)
                {
                    if (taskid == "in")
                    {
                        ctx.Output.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        ctx.Output.WriteLine(pd.GetInputDataXmlSchema());
                    }
                    else if (taskid == "out")
                    {
                        ctx.Output.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        ctx.Output.WriteLine(pd.GetOutputDataXmlSchema());
                    }
                    else if (taskid == "internal")
                    {
                        ctx.Output.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        ctx.Output.WriteLine(pd.GetInternalDataXmlSchema());
                    }
                    else if (taskid == "all_schemas")
                    {
                        ctx.Output.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        ctx.Output.WriteLine(pd.GetAllProcessAndTaskDataSchemas());
                    }
                    else throw new Exception("Unexpected value of schema type: " + taskid);
                }
                else
                {
                    Task tsk = pd.GetTask(taskid);
                    if (dir == "in")
                    {
                        ctx.Output.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        ctx.Output.WriteLine(tsk.GetInputDataXmlSchema());
                    }
                    else if (dir == "out")
                    {
                        ctx.Output.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        ctx.Output.WriteLine(tsk.GetOutputDataXmlSchema());
                    }
                    else if (dir == "internal")
                    {
                        ctx.Output.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        ctx.Output.WriteLine(tsk.GetInternalDataXmlSchema());
                    }
                }
            }
            else
            {
                ctx.Output.WriteLine("Usage: /schema/[definition id]/[task id]/[in|out|internal]");
            }
        }
    }
}
